<?php

class SJB_Admin_Classifieds_ImportUsers extends SJB_Function
{
	public function isAccessible()
	{
		$this->setPermissionLabel('import_users');
		return parent::isAccessible();
	}

	public function execute()
	{
		if (PHP_VERSION_IS_LOWER_THAN) {
			echo PHP_VERSION_IS_LOWER_MESSAGE;
			return null;
		}

		$template_processor = SJB_System::getTemplateProcessor();

		$file_info = isset($_FILES['import_file']) ? $_FILES['import_file'] : null;

		if (empty($file_info) || $file_info['error'] == UPLOAD_ERR_NO_FILE) {
			$errors = array();
			if ($file_info['error'] == UPLOAD_ERR_NO_FILE)
				$errors[] = 'Please choose exl or csv file';

			$user_groups = SJB_UserGroupManager::getAllUserGroupsInfo();

			$template_processor->assign('user_groups', $user_groups);
			$template_processor->assign('errors', $errors);
			$template_processor->display('import_users.tpl');
		}
		else {
			$csv_delimiter = SJB_Request::getVar('csv_delimiter', null);
			$user_group_id = SJB_Request::getVar('user_group_id', null);
            $user_group_sid= SJB_UserGroupManager::getUserGroupSIDByID($user_group_id);
			$extension = $_REQUEST['file_type'];

			if ($extension == 'xls')
				$import_file = new SJB_ImportFileXLS($file_info);
			elseif ($extension == 'csv')
				$import_file = new SJB_ImportFileCSV($file_info, $csv_delimiter);

			$import_file->parse();
			$user = $this->CreateUser(array(), $user_group_id);
			$imported_user_processor = new SJB_ImportedUserProcessor($import_file->getData(), $user);
			$count = 0;
			$errors = array();
			$usernames = array();
			$import_file_url = false;

			while (!$imported_user_processor->isEmpty()) {
				$user_info = $imported_user_processor->getData();
				$user = $this->CreateUser($user_info, $user_group_id);

				foreach ($user->getProperties() as $property) {
					if ($property->id == 'active') {
						$property->type->property_info['value'] = 0;
					}

					if (in_array($property->getType(), array('file', 'logo', 'picture', 'video')) && $property->value !== '') {
						$field_info = SJB_UserProfileFieldManager::getFieldInfoBySID($property->sid);
						if (SJB_UploadFileManager::fileImport($user_info, $field_info)) {
							$import_file_url = true;
						}
					}
				}

				$username = SJB_Array::get($user_info, 'username');
				if (empty($username)) {
					$errors[] = 'Empty username is not allowed, record ignored.';
				}
				elseif (!is_null(SJB_UserManager::getUserSIDbyUsername($username))) {
					$errors[] = '\'' . $user_info['username'] . '\' - this user name already exists, record ignored.';
				}
				else {
					SJB_UserManager::saveUser($user);
                    $isApproveByAdmin = SJB_UserGroupManager::isApproveByAdmin($user_group_sid);
                    if($isApproveByAdmin)
                        SJB_UserManager::setApprovalStatusByUserName($user->getUserName(), 'Pending');
					$count++;
				}
			}

			if ($import_file_url) {
				SJB_Filesystem::delete(SJB_System::getSystemSettings("IMPORT_FILES_DIRECTORY"));
			}

			$template_processor->assign('imported_users_count', $count);
			$template_processor->assign('errors', $errors);
			$template_processor->display('import_users_result.tpl');
		}
	}

	private function CreateUser($user_info, $user_group_id)
	{
		$user_group_sid = SJB_UserGroupManager::getUserGroupSIDByID($user_group_id);
		return new SJB_User($user_info, $user_group_sid);
	}
}